package com.lyt.mall.seckill.service.impl;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.lyt.common.constant.OrderConstant;
import com.lyt.common.constant.SeckillConstant;
import com.lyt.common.dto.SeckillOrderDTO;
import com.lyt.common.utils.R;
import com.lyt.common.vo.MemberVO;
import com.lyt.mall.seckill.dto.SeckillSkuRedisDTO;
import com.lyt.mall.seckill.feign.CouponFeignService;
import com.lyt.mall.seckill.feign.ProductFeignService;
import com.lyt.mall.seckill.interceptor.AuthInterceptor;
import com.lyt.mall.seckill.service.SeckillService;
import com.lyt.mall.seckill.vo.SeckillSessionEntity;
import com.lyt.mall.seckill.vo.SkuInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author: LiuYutong
 */
@Service
@Slf4j
public class SeckillServiceImpl implements SeckillService {

    @Autowired
    CouponFeignService couponFeignService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    ProductFeignService productFeignService;
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    RocketMQTemplate rocketMQTemplate;

    @Trace
    @Override
    public void uploadSeckillSku3Days() {
        //1.通过OpenFeign远程调用Coupon服务中接口来获取未来三天的秒杀活动的商品
        R r = couponFeignService.getLates3DaysSession();
        if (r.getCode() == 0){
            //表示查询操作成功
            String json =(String) r.get("data");
            List<SeckillSessionEntity> seckillSessionEntities = JSON.parseArray(json, SeckillSessionEntity.class);
            //2.上架商品  Redis数据保存
            //缓存商品
            //2.1 缓存每日秒杀的SKU基本信息
            saveSessionInfos(seckillSessionEntities);
            //2.2 缓存每日秒杀的商品信息
            saveSessionSkuInfos(seckillSessionEntities);
        }
    }

    public List<SeckillSkuRedisDTO> blockHandler(BlockException blockException){
        log.error("限流执行的blockHandler方法...{}",blockException.getMessage());
        return null;
    }

    /**
     * 查询当前时间内的秒杀活动及对应的商品SKU信息
     * @return
     */
    @SentinelResource(value = "currentSeckillSkusResources",blockHandler = "blockHandler")
    @Override
    public List<SeckillSkuRedisDTO> getCurrentSeckillSkus() {
        //1.确定当前时间是属于哪个秒杀活动的
        long time = new Date().getTime();
        try(Entry entry = SphU.entry("getCurrentSeckillSkusResources")){
            //被保护的业务逻辑
            //从Redis中查询所有的秒杀活动
            Set<String> keys = stringRedisTemplate.keys(SeckillConstant.SESSION_CHACE_PREFIX + "*");
            for (String key : keys) {
                String replace = key.replace(SeckillConstant.SESSION_CHACE_PREFIX, "");
                String[] s = replace.split("_");
                long start = Long.parseLong(s[0]);   //活动开始的时间
                long end = Long.parseLong(s[1]);     //活动结束的时间
                if (time > start && time < end){
                    //说明当前的秒杀活动就是当前时间需要参与的活动
                    //取出来的是SKU的ID
                    List<String> range = stringRedisTemplate.opsForList().range(key, -100, 100);
                    BoundHashOperations<String, String, String> ops = stringRedisTemplate.boundHashOps(SeckillConstant.SKU_CHACE_PREFIX);
                    List<String> list = ops.multiGet(range);
                    if (list != null && list.size() > 0){
                        List<SeckillSkuRedisDTO> collect = list
                                .stream()
                                .map((item) -> JSON.parseObject(item, SeckillSkuRedisDTO.class))
                                .collect(Collectors.toList());
                        return collect;
                    }
                }
            }
        }catch (BlockException e){
            //资源访问阻止，被限流或被降级
            log.error("getCurrentSeckillSkusResources被限制访问了");
            //
        }



        return null;
    }

    /**
     * 根据skuId查询秒杀活动对应的信息
     * @param skuId
     * @return
     */
    @Override
    public SeckillSkuRedisDTO getSeckillSessionBySkuId(Long skuId) {
        //1.找到所有需要参与秒杀的商品的sku信息
        BoundHashOperations<String, String, String> ops = stringRedisTemplate.boundHashOps(SeckillConstant.SKU_CHACE_PREFIX);
        Set<String> keys = ops.keys();
        if (keys != null && keys.size() > 0){
            String regx = "\\d_" + skuId;
            for (String key : keys) {
                boolean matches = Pattern.matches(regx, key);
                if (matches){
                    //说明找到了对应的SKU信息
                    String json = ops.get(key);
                    SeckillSkuRedisDTO dto = JSON.parseObject(json, SeckillSkuRedisDTO.class);
                    return dto;
                }
            }
        }
        return null;
    }

    /**
     * 实现秒杀逻辑
     * @param killId
     * @param code
     * @param num
     * @return
     */
    @Override
    public String kill(String killId, String code, Integer num) {
        //1.根据killed获取当前秒杀的商品的信息
        BoundHashOperations<String, String, String> ops = stringRedisTemplate.boundHashOps(SeckillConstant.SKU_CHACE_PREFIX);
        String json = ops.get(killId);
        if (StringUtils.isNotBlank(json)){
            SeckillSkuRedisDTO dto = JSON.parseObject(json, SeckillSkuRedisDTO.class);
            //1.校验合法性和时效性
            Long startTime = dto.getStartTime();
            Long endTime = dto.getEndTime();
            long now = new Date().getTime();
            if (now > startTime && now < endTime){
                //说明是在秒杀活动时间范围内的请求
                //2.校验随机码和商品是否合法
                String randCode = dto.getRandCode();
                Long skuId = dto.getSkuId();
                String redisKillId = dto.getPromotionSessionId() + "_" + skuId;
                if (randCode.equals(code) && killId.equals(redisKillId)){
                    //随机码校验合法
                    //3.判断抢购商品数量是否合法
                    if (num <= dto.getSeckillLimit().intValue()){
                        //满足限购的条件
                        //4.判断是否满足幂等性
                        //只要抢购成功就在Redis中存储一条信息：userId + sessionId + skuId
                        MemberVO memberVO = (MemberVO) AuthInterceptor.threadLocal.get();
                        Long id = memberVO.getId();
                        String redisKey = id + "_" + redisKillId;
                        Boolean aBoolean = stringRedisTemplate
                                .opsForValue()
                                .setIfAbsent(redisKey, num.toString(), (endTime - now), TimeUnit.MICROSECONDS);
                        if (Boolean.TRUE.equals(aBoolean)){
                            //表示数据插入成功，是第一次操作
                            RSemaphore semaphore = redissonClient.getSemaphore(SeckillConstant.SKU_STOCK_SEMAPHORE + randCode);
                            try {
                                boolean b = semaphore.tryAcquire(num, 100, TimeUnit.MICROSECONDS);
                                if (b){
                                    //表示秒杀成功
                                    String orderSN = UUID.randomUUID().toString().replace("-","");
                                    //继续完成快速下订单操作 ---> RocketMQ
                                    SeckillOrderDTO orderDTO = new SeckillOrderDTO();
                                    orderDTO.setOrderSN(orderSN);
                                    orderDTO.setSkuId(skuId);
                                    orderDTO.setSeckillPrice(dto.getSeckillPrice());
                                    orderDTO.setMemberId(id);
                                    orderDTO.setNum(num);
                                    orderDTO.setPromotionSessionId(dto.getPromotionSessionId());
                                    //通过RocketMQ发送异步消息
                                    rocketMQTemplate.sendOneWay(OrderConstant.ROCKETMQ_SECKILL_ORDER_TOPIC,
                                            JSON.toJSONString(orderDTO));
                                    return orderSN;
                                }
                            } catch (InterruptedException e) {
                                return null;
                            }
                        }
                    }
                }
            }
        }
        return null;
    }


    /**
     * 保存每日活动的信息到Redis中
     * @param seckillSessionEntities
     */
    private void saveSessionInfos(List<SeckillSessionEntity> seckillSessionEntities) {
        for (SeckillSessionEntity seckillSessionEntity : seckillSessionEntities) {
            //循环每一个活动
            long start = seckillSessionEntity.getStartTime().getTime();
            long end = seckillSessionEntity.getEndTime().getTime();
            //生成key
            String key = SeckillConstant.SESSION_CHACE_PREFIX + start + "_" + end;
            Boolean flag = stringRedisTemplate.hasKey(key);
            if (Boolean.FALSE.equals(flag)){
                //表示这个秒杀活动在Redis中不存在，也就是还没有上架，那么需要保存
                //需要存储到Redis中的这个秒杀活动涉及到的相关的商品信息的SKUID
                List<String> collect = seckillSessionEntity
                        .getRelationEntities()
                        .stream()
                        .map((item) -> item.getPromotionSessionId() + "_" + item.getSkuId().toString())
                        .collect(Collectors.toList());
                stringRedisTemplate.opsForList().leftPushAll(key,collect);
            }
        }
    }


    /**
     * 存储活动对应的SKU信息
     * @param seckillSessionEntities
     */
    private void saveSessionSkuInfos(List<SeckillSessionEntity> seckillSessionEntities) {
        seckillSessionEntities.stream().forEach((session)->{
            //循环取出每个session，然后去出对应的skuId，封装相关信息
            BoundHashOperations<String, Object, Object> hashOps = stringRedisTemplate.boundHashOps(SeckillConstant.SKU_CHACE_PREFIX);
            session.getRelationEntities().stream().forEach((item)->{
                String skuKey = item.getPromotionSessionId() + "_" + item.getSkuId();
                Boolean flag = stringRedisTemplate.hasKey(skuKey);
                if (Boolean.FALSE.equals(flag)){
                    SeckillSkuRedisDTO dto = new SeckillSkuRedisDTO();
                    //1.获取SKU的基本信息
                    R info = productFeignService.info(item.getSkuId());
                    if (info.getCode() == 0){
                        //表示查询成功
                        String json = (String) info.get("skuInfoJSON");
                        dto.setSkuInfoVO(JSON.parseObject(json,SkuInfoVO.class));
                    }
                    //2.获取SKU的秒杀信息
                    BeanUtils.copyProperties(item,dto);
                    //3.设置当前商品的秒杀时间
                    dto.setStartTime(session.getStartTime().getTime());
                    dto.setEndTime(session.getEndTime().getTime());
                    //4.随机码
                    String token = UUID.randomUUID().toString().replace("-","");
                    dto.setRandCode(token);
                    //绑定对应的活动编号
                    dto.setPromotionSessionId(item.getPromotionSessionId());
                    //分布式信号量的处理  限流的目的
                    RSemaphore semaphore = redissonClient.getSemaphore(SeckillConstant.SKU_STOCK_SEMAPHORE + token);
                    //把秒杀活动的商品数量作为分布式信号量的信号量
                    semaphore.trySetPermits(item.getSeckillCount().intValue());

                    hashOps.put(skuKey,JSON.toJSONString(dto));
                }
            });
        });
    }
}
